package com.chinasofti.xian.controller;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class ServiceTwoController {

    //本接口发生异常时，通过熔断器，调用“serviceOneDown”方法
    @HystrixCommand(fallbackMethod = "serviceOneDown")
    @RequestMapping("/serviceTwo")
    @ResponseBody
    public JSONObject serviceTwo(){
        JSONObject ret = new JSONObject();

        ret.put("code", 0);
        ret.put("message", "Service two method return!");

        String str = null;

        str.equals("abc");//人为产生一个空指针异常，引起熔断

        return ret;
    }

    //熔断后调用的方法，直接返回熔断结果
    public JSONObject serviceOneDown(){
        JSONObject ret = new JSONObject();

        ret.put("code", 10500);//模拟发生熔断时的错误代码
        ret.put("message", "ServiceTwo业务模块的“serviceTwo”异常，发生熔断!");

        return ret;
    }
}
